# -*- coding:utf-8 -*-
"""

@File : kafkaDemo.py

@Author: 爸爸

@Date : 2019/7/9

@Desc :kafka pythonDemo   KafkaProducer

"""
import json
import random
import time
from kafka import KafkaProducer
import util

# host = ['127.0.0.1:9092', '120.76.188.219:9092']
host = ['127.0.0.1:9092']
# host = ['10.11.4.24:9092']

statuscode = [0, 1, 2]
alarmcode = [0, 1]
white = [1, 2]
area = ["01", "02", "03", "04"]
imgs = ["a.jpg", "b.jpg", "ss.jpg"]
s = "1234567890qwertyuiopasdfghjklzxcvbnm" * 6
s = list(s)


def GDYTStatus():
    '''
    广电运通一体机
    :return:
    '''
    data = {"deviceType": 1,
            "msgSendTime": util.GetTime_yyyyMMddHHmmss(time.time()),
            "datas": [
                {"deviceId": "gdyt10001",
                 "status": random.choice(statuscode),
                 "msgCreateTime": util.GetTime_yyyyMMddHHmmss(time.time())
                 }
            ]
            }
    return "ShareDevStatus", data


def ThzStatus():
    data = {"deviceType": 5,
            "msgSendTime": util.GetTime_yyyyMMddHHmmss(time.time()),
            "datas": [
                {"deviceId": "thz040001",
                 "status": random.choice(statuscode),
                 "msgCreateTime": util.GetTime_yyyyMMddHHmmss(time.time())
                 }
            ]
            }
    return "ShareDevStatus", data


def GDYTCheck():
    timestr = util.GetTime_yyyyMMddHHmmss(time.time())
    random.shuffle(s)
    data = {
        "deviceType": 1,
        "datas": {
            "deviceId": "gdyt10001",
            "eventId": "".join(s[0:34]),
            "msgCreateTime": timestr,
            # "warning": 1,
            # "warnArea": "01",
            "picture1": random.choice(imgs),
            "picture2": "",
            "picture3": "",
            "picture4": "",
            "white": random.choice(white)
        },
        "msgSendTime": timestr
    }
    if random.choice(alarmcode) == 0:
        data["datas"]["warning"] = 0
        data["datas"]["warnArea"] = "00"
    else:
        data["datas"]["warning"] = 1
        data["datas"]["warnArea"] = random.choice(area)
    return "ShareCheckInfo", data


def ThzCheck():
    timestr = util.GetTime_yyyyMMddHHmmss(time.time())
    random.shuffle(s)
    data = {
        "deviceType": 5,
        "datas": {
            "deviceId": "thz050001",
            "eventId": "".join(s[0:34]),
            "msgCreateTime": timestr,
            # "warning": 1,
            # "warnArea": "01",
            "picture1": random.choice(imgs),
            "picture2": random.choice(imgs),
            "picture3": random.choice(imgs),
            "picture4": random.choice(imgs),
            "white": random.choice(white)
        },
        "msgSendTime": timestr
    }
    if random.choice(alarmcode) == 0:
        data["datas"]["warning"] = 0
        data["datas"]["warnArea"] = "00"
    else:
        data["datas"]["warning"] = 1
        data["datas"]["warnArea"] = random.choice(area)
    return "ShareCheckInfo", data


def main():
    producer = KafkaProducer(bootstrap_servers=host, value_serializer=lambda v: json.dumps(v).encode("utf-8"), api_version=(0, 10))
    funcs = [GDYTStatus, GDYTCheck, ThzStatus, ThzCheck]
    with open("kafkaData.txt", 'a+') as f:
        while True:
            for func in funcs:
                topic, data = func()
                producer.send(topic, data)
                producer.flush(60)
                f.write(json.dumps(data))
                f.write("\r\n")
                f.flush()
                print(data)
            time.sleep(5)
            pass


if __name__ == '__main__':
    main()
    # for i in range(10):
    #     print(ThzCheck())
    pass
